﻿using System;
using System.Data.Common;
using System.Xml.Serialization;
using System.Data;
using System.ComponentModel;

namespace ElectronicCommerce.Framework.Data.Configuration
{
    [Serializable]
    public class DataOperationCommand : ICloneable
    {
        private string commandText;

        private DataOperationsParametersList parametersField;

        private string name;

        private string connectionStringName;

        private CommandType commandType;

        private DatabaseTypeEnum databaseType;

        private int timeOut;

        public DataOperationCommand()
        {
            commandType = CommandType.Text;
            timeOut = 300;
            databaseType = DatabaseTypeEnum.Query;
        }

        [XmlElement("commandText")]
        public string CommandText
        {
            get
            {
                return commandText;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    return;
                }
                commandText = value.Trim();
            }
        }

        [XmlElement("parameters")]
        public DataOperationsParametersList Parameters
        {
            get
            {
                return parametersField;
            }
            set
            {
                parametersField = value;
            }
        }

        [XmlAttribute("name")]
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        [XmlAttribute("connectionStringName")]
        public string ConnectionStringName
        {
            get
            {
                return connectionStringName;
            }
            set
            {
                connectionStringName = value;
            }
        }

        [DefaultValueAttribute(CommandType.Text)]
        [XmlAttribute("commandType")]
        public CommandType CommandType
        {
            get
            {
                return commandType;
            }
            set
            {
                commandType = value;
            }
        }

        [DefaultValue(DatabaseTypeEnum.Transaction)]
        [XmlAttribute("databaseType")]
        public DatabaseTypeEnum DatabaseType
        {
            get
            {
                return databaseType;
            }
            set
            {
                databaseType = value;
            }
        }

        [DefaultValueAttribute(300)]
        [XmlAttribute("timeOut")]
        public int TimeOut
        {
            get
            {
                return timeOut;
            }
            set
            {
                timeOut = value;
            }
        }

        public DataCommand GetDataCommand()
        {
            return new DataCommand(this);
        }

        private DbCommand GetDbCommandByFactory()
        {
            DbCommand command = DbCommandFactory.CreateDbCommand();

            command.CommandText = CommandText;
            command.CommandTimeout = TimeOut;
            command.CommandType = (CommandType)Enum.Parse(typeof(CommandType), CommandType.ToString());

            // todo: populate command
            if (Parameters != null && Parameters.ParameterList != null && Parameters.ParameterList.Length > 0)
            {
                foreach (DataOperationParameter param in Parameters.ParameterList)
                {
                    command.Parameters.Add(param.GetDbParameter());
                }
            }

            return command;
        }

        #region ICloneable Members

        public object Clone()
        {
            DataOperationCommand cmd = new DataOperationCommand();
            if (!string.IsNullOrEmpty(this.ConnectionStringName))
            {
                cmd.ConnectionStringName = this.ConnectionStringName.Clone().ToString();
            }
            cmd.CommandType = this.CommandType;
            cmd.CommandText = this.CommandText.Clone().ToString();
            cmd.Name = this.Name.Clone().ToString();
            cmd.TimeOut = this.TimeOut;
            if (this.Parameters != null)
            {
                cmd.Parameters = (DataOperationsParametersList)this.Parameters.Clone();
            }

            return cmd;
        }

        #endregion
    }
}
